package com.bw.base.mvvm

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.ViewDataBinding
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import java.lang.reflect.ParameterizedType

abstract class BaseFram<VDB:ViewDataBinding,VM:BaseViewModel>:Fragment() {
    private val type by lazy {
        //取出泛型参数列表
        (javaClass.genericSuperclass as ParameterizedType) .actualTypeArguments
    }
    protected val binding by lazy {
        var clazz=type[0] as Class<VDB>
        var method=clazz.getMethod("inflate",LayoutInflater::class.java)
        method.invoke(null,layoutInflater) as VDB
    }
    protected val viewmodel by lazy {
        val clazz=type[1] as Class<VM>
        ViewModelProvider(this)[clazz]
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {

        intitView()
        intitData()
        return binding.root
    }

    abstract fun intitView()

    abstract fun intitData()
}